﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Management;
using System.IO;
using System.Security.Principal;
using System.Security.AccessControl;

namespace AC
{
	public class Lic
	{
		public int CheckLicense()
		{
			byte[] arrReg = null;
			byte[] arrFile1 = null;
			byte[] arrFile2 = null;
			byte[] arrFile3 = null;

			int returnValue = -1; //-1:invalid trial; -2:end of trial; -3:active; -4:fisrt run; >0:remain days
			int i = 0;
			string path = null;

			#region check registry
			RegistryKey rk = Registry.LocalMachine;
			RegistryKey sk = rk.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment\");
			if (sk != null)
			{
				try
				{
					arrReg = (byte[])sk.GetValue("windows_tracing_bin");
				}
				catch (Exception ex)
				{
					try
					{
						File.AppendAllText(RMSM.Session.AppPath + "bug.txt", DateTime.Now.ToString("dd/MM/yyyy  HH:mm:ss") + " : " + ex.Message + "\r\n\r\n" + ex.StackTrace + "\r\n----------------\r\n", Encoding.UTF8);
					}
					catch { }
				}
			}
			#endregion check registry

			#region read file 1
			try
			{
				path = Environment.SystemDirectory;
				if (path.EndsWith("\\") == false)
					path += "\\";
				path += "mcmpt.tlb";

				if (File.Exists(path))
					arrFile1 = File.ReadAllBytes(path);
			}
			catch (Exception ex)
			{
				try
				{
					File.AppendAllText(RMSM.Session.AppPath + "bug.txt", DateTime.Now.ToString("dd/MM/yyyy  HH:mm:ss") + " : " + ex.Message + "\r\n\r\n" + ex.StackTrace + "\r\n----------------\r\n", Encoding.UTF8);
				}
				catch { }
			}
			#endregion read file 1

			#region read file 2
			try
			{
				path = Environment.SystemDirectory;
				if (path.EndsWith("\\"))
					path = path.Substring(0, path.Length - 1);

				i = path.LastIndexOf("\\");
				path = path.Substring(0, i);

				if (path.EndsWith("\\") == false)
					path += "\\";
				path += "wnhlp.dat";

				if (File.Exists(path))
					arrFile2 = File.ReadAllBytes(path);
			}
			catch (Exception ex)
			{
				try
				{
					File.AppendAllText(RMSM.Session.AppPath + "bug.txt", DateTime.Now.ToString("dd/MM/yyyy  HH:mm:ss") + " : " + ex.Message + "\r\n\r\n" + ex.StackTrace + "\r\n----------------\r\n", Encoding.UTF8);
				}
				catch { }
			}
			#endregion read file 2

			#region read file 3
			try
			{
				path = Environment.SystemDirectory;
				if (path.EndsWith("\\"))
					path = path.Substring(0, path.Length - 1);

				i = path.LastIndexOf("\\");
				path = path.Substring(0, i);

				if (path.EndsWith("\\") == false)
					path += "\\";
				path += "Cursors\\";
				if (Directory.Exists(path) == false)
					Directory.CreateDirectory(path);

				path += "lng.cux";

				if (File.Exists(path))
					arrFile3 = File.ReadAllBytes(path);
			}
			catch (Exception ex)
			{
				try
				{
					File.AppendAllText(RMSM.Session.AppPath + "bug.txt", DateTime.Now.ToString("dd/MM/yyyy  HH:mm:ss") + " : " + ex.Message + "\r\n\r\n" + ex.StackTrace + "\r\n----------------\r\n", Encoding.UTF8);
				}
				catch { }
			}
			#endregion read file 3

			bool nullReg = (arrReg == null);
			bool nullFile1 = (arrFile1 == null);
			bool nullFile2 = (arrFile2 == null);
			bool nullFile3 = (arrFile3 == null);

			object[] arrObj = null;
			DateTime dtBeginReg = DateTime.MinValue;
			DateTime dtEndReg = DateTime.MinValue;
			DateTime dtLastAccessReg = DateTime.MinValue;
			int countReg = 0;
			string keyReg = "";
			string codeReg = "";

			DateTime dtBegin1 = DateTime.MinValue;
			DateTime dtEnd1 = DateTime.MinValue;
			DateTime dtLastAccess1 = DateTime.MinValue;
			int count1 = 0;
			string key1 = "";
			string code1 = "";

			DateTime dtBegin2 = DateTime.MinValue;
			DateTime dtEnd2 = DateTime.MinValue;
			DateTime dtLastAccess2 = DateTime.MinValue;
			int count2 = 0;
			string key2 = "";
			string code2 = "";

			DateTime dtBegin3 = DateTime.MinValue;
			DateTime dtEnd3 = DateTime.MinValue;
			DateTime dtLastAccess3 = DateTime.MinValue;
			int count3 = 0;
			string key3 = "";
			string code3 = "";

			if (nullReg && nullFile1 && nullFile2 && nullFile3)
			{
				//nếu là lần chạy đầu tiên thì sẽ ghi license mới
				returnValue = -4;
				WriteNewLicense();
			}
			else
				if (!nullReg && !nullFile1 && !nullFile2 && !nullFile3)
				{
					#region get info
					arrObj = ReadKey(arrReg);
					if (arrObj == null)
					{
						returnValue = -1;
						WriteErrorLicense();
						return returnValue;
					}
					dtBeginReg = (DateTime)arrObj[0];
					dtEndReg = (DateTime)arrObj[1];
					dtLastAccessReg = (DateTime)arrObj[2];
					countReg = (int)arrObj[3];
					keyReg = (string)arrObj[4];
					codeReg = (string)arrObj[5];

					arrObj = ReadKey(arrFile1);
					if (arrObj == null)
					{
						returnValue = -1;
						WriteErrorLicense();
						return returnValue;
					}
					dtBegin1 = (DateTime)arrObj[0];
					dtEnd1 = (DateTime)arrObj[1];
					dtLastAccess1 = (DateTime)arrObj[2];
					count1 = (int)arrObj[3];
					key1 = (string)arrObj[4];
					code1 = (string)arrObj[5];


					arrObj = ReadKey(arrFile2);
					if (arrObj == null)
					{
						returnValue = -1;
						WriteErrorLicense();
						return returnValue;
					}
					dtBegin2 = (DateTime)arrObj[0];
					dtEnd2 = (DateTime)arrObj[1];
					dtLastAccess2 = (DateTime)arrObj[2];
					count2 = (int)arrObj[3];
					key2 = (string)arrObj[4];
					code2 = (string)arrObj[5];


					arrObj = ReadKey(arrFile3);
					if (arrObj == null)
					{
						returnValue = -1;
						WriteErrorLicense();
						return returnValue;
					}
					dtBegin3 = (DateTime)arrObj[0];
					dtEnd3 = (DateTime)arrObj[1];
					dtLastAccess3 = (DateTime)arrObj[2];
					count3 = (int)arrObj[3];
					key3 = (string)arrObj[4];
					code3 = (string)arrObj[5];
					#endregion get info


					//nếu các giá trị không khớp nhau thì sẽ khóa trial. yêu cầu phải đăng ký
					#region check invalid
					if ((dtBeginReg.ToString("ddMMyyyy") != dtBegin1.ToString("ddMMyyyy"))
						|| (dtBeginReg.ToString("ddMMyyyy") != dtBegin2.ToString("ddMMyyyy"))
						|| (dtBeginReg.ToString("ddMMyyyy") != dtBegin3.ToString("ddMMyyyy")))
					{
						returnValue = -1;
						WriteErrorLicense();
						return returnValue;
					}

					if ((dtEndReg.ToString("ddMMyyyy") != dtEnd1.ToString("ddMMyyyy"))
						|| (dtEndReg.ToString("ddMMyyyy") != dtEnd2.ToString("ddMMyyyy"))
						|| (dtEndReg.ToString("ddMMyyyy") != dtEnd3.ToString("ddMMyyyy")))
					{
						returnValue = -1;
						WriteErrorLicense();
						return returnValue;
					}

					if ((dtLastAccessReg.ToString("ddMMyyyy") != dtLastAccess1.ToString("ddMMyyyy"))
						|| (dtLastAccessReg.ToString("ddMMyyyy") != dtLastAccess2.ToString("ddMMyyyy"))
						|| (dtLastAccessReg.ToString("ddMMyyyy") != dtLastAccess3.ToString("ddMMyyyy")))
					{
						returnValue = -1;
						WriteErrorLicense();
						return returnValue;
					}

					if ((countReg != count1)
						|| (countReg != count2)
						|| (countReg != count3))
					{
						returnValue = -1;
						WriteErrorLicense();
						return returnValue;
					}

					if ((keyReg != key1)
						|| (keyReg != key2)
						|| (keyReg != key3))
					{
						returnValue = -1;
						WriteErrorLicense();
						return returnValue;
					}

					if ((codeReg != code1)
						|| (codeReg != code2)
						|| (codeReg != code3))
					{
						returnValue = -1;
						WriteErrorLicense();
						return returnValue;
					}
					#endregion check invalid


					if (countReg == 5)
					{
						//=5: trial đã bị khóa
						//invalid trial
						returnValue = -1;
						return returnValue;
					}
					else
						if (countReg == 2)
						{
							//đã nhập license và số ngày cách nhau sẽ là 100 ngày
							if (dtEndReg.Subtract(dtBeginReg).TotalDays == 100)
							{
								if (keyReg == CreateLicense() && codeReg == CreateActiceCode(keyReg))
								{
									//nếu tất cả trùng khớp thì là đã kích hoạt
									returnValue = -3;
									return returnValue;
								}
								else
								{
									//nếu key và code không đúng thì khóa trial
									returnValue = -1;
									WriteErrorLicense();
									return returnValue;
								}
							}
							else
							{
								//nếu không cách nhau 100 ngày thì sẽ bị khóa
								returnValue = -1;
								WriteErrorLicense();
								return returnValue;
							}
						}
						else
						{
							DateTime dtNow = RMSM.StaticUtil.RebuiltDateTime(DateTime.Now, 0, 0);
							int iNow = int.Parse(dtNow.ToString("yyyyMMdd"));
							int iBeginReg = int.Parse(dtBeginReg.ToString("yyyyMMdd"));
							int iEndReg = int.Parse(dtEndReg.ToString("yyyyMMdd"));

							if (dtEndReg.Subtract(dtBeginReg).TotalDays != 31)
							{
								//Nếu ngày bắt đầu và ngày kết thúc khác 31 ngày -> khóa trial
								returnValue = -1;
								WriteErrorLicense();
								return returnValue;
							}
							else
								if (iEndReg <= iNow)
								{
									//nếu ngày kết thúc nhỏ hơn hoặc bằng ngày hiện tại -> hết hạn dùng trial
									returnValue = -2;
									return returnValue;
								}
								else
									if (iBeginReg > iNow)
									{
										//nếu ngày bắt đầu lớn hơn ngày hiện tại -> khóa trial
										returnValue = -1;
										WriteErrorLicense();
										return returnValue;
									}
									else
										if (int.Parse(dtLastAccessReg.ToString("yyyyMMdd")) > iNow)
										{
											//nếu ngày truy cập cuối cùng lớn hơn ngày hiện tại -> khóa trial
											returnValue = -1;
											WriteErrorLicense();
											return returnValue;
										}
										else
										{
											//update
											DateTime dtLastAccess = DateTime.Now;
											WriteRegistry(CreateKey(dtBeginReg, dtEndReg, dtLastAccess, countReg, keyReg, codeReg));
											WriteFile1(CreateKey(dtBeginReg, dtEndReg, dtLastAccess, countReg, keyReg, codeReg));
											WriteFile2(CreateKey(dtBeginReg, dtEndReg, dtLastAccess, countReg, keyReg, codeReg));
											WriteFile3(CreateKey(dtBeginReg, dtEndReg, dtLastAccess, countReg, keyReg, codeReg));

											//trả về số ngày còn lại
											returnValue = Convert.ToInt32(dtEndReg.Subtract(dtNow).TotalDays);
											return returnValue;
										}
						}
				}

			return returnValue;
		}

		public int CheckLicense(string skey, string scode)
		{
			int returnValue = -1; //-1:invalid code; >=0:ok

			if (string.IsNullOrEmpty(skey) || string.IsNullOrEmpty(scode))
				return returnValue;

			int i = 0;
			string key = "";
			for (i = 0; i < skey.Length; i++)
				if (skey[i] >= '0' && skey[i] <= '9')
					key += skey[i].ToString();

			string code = "";
			for (i = 0; i < scode.Length; i++)
				if (scode[i] >= '0' && scode[i] <= '9')
					code += scode[i].ToString();

			if (key.Length != 16)
				return -1;

			if (code.Length != 16)
				return -1;

			if (key == CreateLicense() && code == CreateActiceCode(key))
			{
				WriteOKLicense(key, code);
				Random random = new Random();
				returnValue = random.Next(1, 100000);
			}
			else
			{
				returnValue = -1;
			}

			return returnValue;
		}

		private bool WriteRegistry(byte[] arrByte)
		{
			try
			{
				RegistryKey rk2 = Registry.LocalMachine;
				RegistryKey sk2 = rk2.CreateSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment\");
				sk2.SetValue("windows_tracing_bin", arrByte, RegistryValueKind.Binary);
				return true;
			}
			catch (Exception ex)
			{
				try
				{
					File.AppendAllText(RMSM.Session.AppPath + "bug.txt", DateTime.Now.ToString("dd/MM/yyyy  HH:mm:ss") + " : " + ex.Message + "\r\n\r\n" + ex.StackTrace + "\r\n----------------\r\n", Encoding.UTF8);
				}
				catch { }

				return false;
			}
		}

		private bool WriteFile1(byte[] arrByte)
		{
			try
			{
				string path = Environment.SystemDirectory;
				if (path.EndsWith("\\") == false)
					path += "\\";
				path += "mcmpt.tlb";

				File.WriteAllBytes(path, arrByte);
				return true;
			}
			catch (Exception ex)
			{
				try
				{
					File.AppendAllText(RMSM.Session.AppPath + "bug.txt", DateTime.Now.ToString("dd/MM/yyyy  HH:mm:ss") + " : " + ex.Message + "\r\n\r\n" + ex.StackTrace + "\r\n----------------\r\n", Encoding.UTF8);
				}
				catch { }

				return false;
			}
		}

		private bool WriteFile2(byte[] arrByte)
		{
			try
			{
				string path = Environment.SystemDirectory;
				if (path.EndsWith("\\"))
					path = path.Substring(0, path.Length - 1);

				int i = path.LastIndexOf("\\");
				path = path.Substring(0, i);

				if (path.EndsWith("\\") == false)
					path += "\\";
				path += "wnhlp.dat";

				File.WriteAllBytes(path, arrByte);
				return true;
			}
			catch (Exception ex)
			{
				try
				{
					File.AppendAllText(RMSM.Session.AppPath + "bug.txt", DateTime.Now.ToString("dd/MM/yyyy  HH:mm:ss") + " : " + ex.Message + "\r\n\r\n" + ex.StackTrace + "\r\n----------------\r\n", Encoding.UTF8);
				}
				catch { }

				return false;
			}
		}

		private bool WriteFile3(byte[] arrByte)
		{
			try
			{
				string path = Environment.SystemDirectory;
				if (path.EndsWith("\\"))
					path = path.Substring(0, path.Length - 1);

				int i = path.LastIndexOf("\\");
				path = path.Substring(0, i);

				if (path.EndsWith("\\") == false)
					path += "\\";
				path += "Cursors\\";
				if (Directory.Exists(path) == false)
					Directory.CreateDirectory(path);
				path += "lng.cux";

				File.WriteAllBytes(path, arrByte);
				return true;
			}
			catch (Exception ex)
			{
				try
				{
					File.AppendAllText(RMSM.Session.AppPath + "bug.txt", DateTime.Now.ToString("dd/MM/yyyy  HH:mm:ss") + " : " + ex.Message + "\r\n\r\n" + ex.StackTrace + "\r\n----------------\r\n", Encoding.UTF8);
				}
				catch { }

				return false;
			}
		}

		private byte[] CreateKey(DateTime dtBegin, DateTime dtEnd, DateTime dtLastAccess, int value, string key, string code)
		{
			//ddMMyyyyddMMyyyyddMMyyyyIaaaabbbbccccdddd1111222233334444
			int i = 0;
			Random random = new Random();
			byte[] arrOffset = new byte[57];
			for (i = 0; i < 57; i++)
				arrOffset[i] = Convert.ToByte(random.Next(0, 57));

			byte[] arrInfo = new byte[57];
			i = -1;
			arrInfo[++i] = Convert.ToByte(dtBegin.Day / 10);
			arrInfo[++i] = Convert.ToByte(dtBegin.Day % 10);
			arrInfo[++i] = Convert.ToByte(dtBegin.Month / 10);
			arrInfo[++i] = Convert.ToByte(dtBegin.Month % 10);
			arrInfo[++i] = Convert.ToByte(dtBegin.Year / 1000);
			arrInfo[++i] = Convert.ToByte(dtBegin.Year % 1000 / 100);
			arrInfo[++i] = Convert.ToByte(dtBegin.Year % 100 / 10);
			arrInfo[++i] = Convert.ToByte(dtBegin.Year % 10);

			arrInfo[++i] = Convert.ToByte(dtEnd.Day / 10);
			arrInfo[++i] = Convert.ToByte(dtEnd.Day % 10);
			arrInfo[++i] = Convert.ToByte(dtEnd.Month / 10);
			arrInfo[++i] = Convert.ToByte(dtEnd.Month % 10);
			arrInfo[++i] = Convert.ToByte(dtEnd.Year / 1000);
			arrInfo[++i] = Convert.ToByte(dtEnd.Year % 1000 / 100);
			arrInfo[++i] = Convert.ToByte(dtEnd.Year % 100 / 10);
			arrInfo[++i] = Convert.ToByte(dtEnd.Year % 10);

			arrInfo[++i] = Convert.ToByte(dtLastAccess.Day / 10);
			arrInfo[++i] = Convert.ToByte(dtLastAccess.Day % 10);
			arrInfo[++i] = Convert.ToByte(dtLastAccess.Month / 10);
			arrInfo[++i] = Convert.ToByte(dtLastAccess.Month % 10);
			arrInfo[++i] = Convert.ToByte(dtLastAccess.Year / 1000);
			arrInfo[++i] = Convert.ToByte(dtLastAccess.Year % 1000 / 100);
			arrInfo[++i] = Convert.ToByte(dtLastAccess.Year % 100 / 10);
			arrInfo[++i] = Convert.ToByte(dtLastAccess.Year % 10);

			arrInfo[++i] = Convert.ToByte(value);


			byte[] arrKey = Encoding.ASCII.GetBytes(key);
			byte[] arrCode = Encoding.ASCII.GetBytes(code);

			for (i = 0; i < 16; i++)
			{
				arrInfo[i + 25] = arrKey[i];
				arrInfo[i + 41] = arrCode[i];
			}

			byte[] arrReturn = new byte[114];
			int j = -1;
			for (i = 0; i < 57; i++)
			{
				arrReturn[++j] = Convert.ToByte(arrOffset[i] + arrInfo[i]);
				arrReturn[++j] = arrOffset[i];
			}

			return arrReturn;
		}

		private object[] ReadKey(byte[] arrByte)
		{
			try
			{
				int i = 0;
				int j = -1;
				byte[] arrInfo = new byte[57];
				for (i = 0; i < 57; i++)
					arrInfo[i] = Convert.ToByte(arrByte[++j] - arrByte[++j]);

				i = -1;
				int day = (arrInfo[++i] * 10) + arrInfo[++i];
				int month = (arrInfo[++i] * 10) + arrInfo[++i];
				int year = (arrInfo[++i] * 1000) + (arrInfo[++i] * 100) + (arrInfo[++i] * 10) + arrInfo[++i];
				DateTime dtBegin = new DateTime(year, month, day, 0, 0, 0, 0);

				day = (arrInfo[++i] * 10) + arrInfo[++i];
				month = (arrInfo[++i] * 10) + arrInfo[++i];
				year = (arrInfo[++i] * 1000) + (arrInfo[++i] * 100) + (arrInfo[++i] * 10) + arrInfo[++i];
				DateTime dtEnd = new DateTime(year, month, day, 0, 0, 0, 0);

				day = (arrInfo[++i] * 10) + arrInfo[++i];
				month = (arrInfo[++i] * 10) + arrInfo[++i];
				year = (arrInfo[++i] * 1000) + (arrInfo[++i] * 100) + (arrInfo[++i] * 10) + arrInfo[++i];
				DateTime dtLastAccess = new DateTime(year, month, day, 0, 0, 0, 0);

				int count = arrInfo[++i];

				string key = Encoding.ASCII.GetString(arrInfo, 25, 16);
				string code = Encoding.ASCII.GetString(arrInfo, 41, 16);

				return new object[] { dtBegin, dtEnd, dtLastAccess, count, key, code };
			}
			catch (Exception ex)
			{
				try
				{
					File.AppendAllText(RMSM.Session.AppPath + "bug.txt", DateTime.Now.ToString("dd/MM/yyyy  HH:mm:ss") + " : " + ex.Message + "\r\n\r\n" + ex.StackTrace + "\r\n----------------\r\n", Encoding.UTF8);
				}
				catch { }

				return null;
			}
		}

		private bool WriteNewLicense()
		{
			bool ok = true;
			DateTime dtBegin = DateTime.Now;
			DateTime dtEnd = dtBegin.AddDays(31);
			DateTime dtLastAccess = dtBegin;
			int value = 0;
			string key = CreateLicense();
			string code = "0000000000000000";

			ok |= WriteRegistry(CreateKey(dtBegin, dtEnd, dtLastAccess, value, key, code));
			ok |= WriteFile1(CreateKey(dtBegin, dtEnd, dtLastAccess, value, key, code));
			ok |= WriteFile2(CreateKey(dtBegin, dtEnd, dtLastAccess, value, key, code));
			ok |= WriteFile3(CreateKey(dtBegin, dtEnd, dtLastAccess, value, key, code));

			return ok;
		}

		private bool WriteErrorLicense()
		{
			bool ok = true;
			DateTime dtBegin = DateTime.Now;
			DateTime dtEnd = dtBegin.AddDays(31);
			DateTime dtLastAccess = dtBegin;
			int value = 5;
			string key = CreateLicense();
			string code = "0000000000000000";

			ok |= WriteRegistry(CreateKey(dtBegin, dtEnd, dtLastAccess, value, key, code));
			ok |= WriteFile1(CreateKey(dtBegin, dtEnd, dtLastAccess, value, key, code));
			ok |= WriteFile2(CreateKey(dtBegin, dtEnd, dtLastAccess, value, key, code));
			ok |= WriteFile3(CreateKey(dtBegin, dtEnd, dtLastAccess, value, key, code));

			return ok;
		}

		private bool WriteOKLicense(string key, string code)
		{
			bool ok = true;
			DateTime dtBegin = DateTime.Now;
			DateTime dtEnd = dtBegin.AddDays(100);
			DateTime dtLastAccess = dtBegin;
			int value = 2;

			ok |= WriteRegistry(CreateKey(dtBegin, dtEnd, dtLastAccess, value, key, code));
			ok |= WriteFile1(CreateKey(dtBegin, dtEnd, dtLastAccess, value, key, code));
			ok |= WriteFile2(CreateKey(dtBegin, dtEnd, dtLastAccess, value, key, code));
			ok |= WriteFile3(CreateKey(dtBegin, dtEnd, dtLastAccess, value, key, code));

			return ok;
		}

		public string CreateLicense()
		{
			string ProcessorId = SearchInfo("ProcessorId", "Win32_Processor");
			string SystemName = SearchInfo("SystemName", "Win32_Processor");
			string BB_SerialNumber = SearchInfo("SerialNumber", "Win32_BaseBoard");
			string BB_Product = SearchInfo("Product", "Win32_BaseBoard");
			string BIOS_SerialNumber = SearchInfo("SerialNumber", "Win32_BIOS");
			string HDD_Caption = SearchInfo("Caption", "Win32_DiskDrive");
			string HDD_Model = SearchInfo("Model", "Win32_DiskDrive");
			string HDD_SerialNumber = SearchInfo("SerialNumber", "Win32_DiskDrive");
			string HDD_Signature = SearchInfo("Signature", "Win32_DiskDrive");
			string str = ProcessorId + SystemName + BB_SerialNumber + BB_Product + BIOS_SerialNumber + HDD_Caption + HDD_Model + HDD_SerialNumber + HDD_Signature;

			string returnValue = "0000000000000000";
			if (str != "")
			{
				byte[] arrByte = Encoding.ASCII.GetBytes(str);
				int i = 0;
				int length = arrByte.Length;
				long lvalue = 0;
				bool flag = true;

				while (lvalue < 10000000000000000)
				{
					for (i = 0; i < length; i++)
					{
						if (arrByte[i] > 0)
						{
							if (flag)
								lvalue += Convert.ToInt64(arrByte[i]);
							else
								lvalue *= Convert.ToInt64(arrByte[i]);

							if (lvalue >= 10000000000000000 && lvalue >= 0)
								break;

							flag = !flag;
						}
					}
				}

				returnValue = lvalue.ToString().Substring(0, 16);
			}

			return returnValue;
		}

		private string CreateActiceCode(string key)
		{
			string returnValue = "0000000000000000";
			if (key == null)
				key = CreateLicense();

			if (key.Length != 16)
				return returnValue;

			if (key != "")
			{
				byte[] arrByte = Encoding.ASCII.GetBytes(key);
				int i = 0;
				int length = arrByte.Length;
				long lvalue = 0;
				bool flag = true;

				while (lvalue < 10000000000000000)
				{
					for (i = 0; i < length; i++)
					{
						if (arrByte[i] > 0)
						{
							if (flag)
								lvalue += Convert.ToInt64(arrByte[i]);
							else
								lvalue *= Convert.ToInt64(arrByte[i]);

							if (lvalue >= 10000000000000000 && lvalue >= 0)
								break;

							flag = !flag;
						}
					}
				}

				returnValue = lvalue.ToString().Substring(0, 16);
			}

			return returnValue;
		}

		private string SearchInfo(string col, string table)
		{
			string value = "AXYZ";
			try
			{
				ManagementObjectSearcher searcher = new ManagementObjectSearcher("select " + col + " from " + table);
				foreach (ManagementObject share in searcher.Get())
				{
					foreach (PropertyData PC in share.Properties)
					{
						value += PC.Value;
					}
				}
			}
			catch (Exception ex)
			{
				try
				{
					File.AppendAllText(RMSM.Session.AppPath + "bug.txt", DateTime.Now.ToString("dd/MM/yyyy  HH:mm:ss") + " : " + "select " + col + " from " + table + "\r\n-------------\r\n" + ex.Message + "\r\n", Encoding.UTF8);
				}
				catch { }
			}

			return value;
		}
	}
}
